/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_ssap_server_callback_proxy.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
int32_t SleSsapServerCallbackProxy::InnerTransact(
    uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
{
    if(code != SLE_SSPA_SERVER_CALLBACK_WRITE_REQUEST) {
        SLE_LOGD("InnerTransact In");
    }
    auto remote = Remote();
    if (remote == nullptr) {
        SLE_LOGE("get remote failed, error code %{public}d", code);
        return OBJECT_NULL;
    }
    int32_t err = remote->SendRequest(code, data, reply, flags);
    if (err != NO_ERROR) {
        SLE_LOGE("send request failed, ipcErr=%{public}d code %{public}d", err, code);
    }
    return err;
}

void SleSsapServerCallbackProxy::OnStartService(uint8_t serverId, uint16_t handle, uint32_t errCode)
{
    SLE_LOGD("OnStartService In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapServerCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(serverId), "write server id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(handle), "write service handle failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_START_SERVICE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapServerCallbackProxy::OnAllServiceDelete(uint8_t serverId, uint32_t errCode)
{
    SLE_LOGD("OnAllServiceDelete In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapServerCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(serverId), "write server id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_DELETE_ALL_SERVICES, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapServerCallbackProxy::OnReadRequest(uint8_t serverId, uint16_t connectId, const SsapServerReqReadCbParam &param, uint32_t errCode)
{
    SLE_LOGD("OnReadRequest In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapServerCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(serverId), "write server id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&param), "write read request param failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_READ_REQUEST, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapServerCallbackProxy::OnWriteRequest(uint8_t serverId, uint16_t connectId, const SsapServerReqWriteCbParam &param, uint32_t errCode)
{
#if 0
    SLE_LOGD("OnWriteRequest In");
#endif
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapServerCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(serverId), "write server id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&param), "write write request param failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_WRITE_REQUEST, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapServerCallbackProxy::OnMtuInfoChanged(uint8_t serverId, uint16_t connectId, uint32_t mtu, uint16_t version, uint32_t errCode)
{
    SLE_LOGD("OnMtuInfoChanged In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapServerCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(serverId), "write server id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(mtu), "write mtu failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(version), "write version failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_MTU_INFO_CHANGED, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}
} // SleStandard
} // NearLink
} // OHOS